/**
 * Copyright (c) 2011 Tomi Paananen.
 *
 * Part of Quick Gestures engine library.
 */

#ifndef QGUTILS_H
#define QGUTILS_H

// Qt includes
#include <QList>
#include <QPoint>
#include <QPointF>
#include <QtCore/qmath.h>
#include <QtGlobal>

// qgengine includes
#include "gesture.h"
#include "qgcommon.h"
#include "qgtrace.h"


/*!
  \class QGUtils
  \brief Contains a set of utility functions.
*/


class QGUtils
{
public:

    /*!
      Extracts the minimum and maximum points from both axis from the set of
      points contained by \a points. The extracted minimum and maximum values
      are stored in the given attributes.
    */
    static inline void getMinMaxXY(const PointList &points,
                                   int &minX, int &minY, int &maxX, int &maxY)
    {
        const int pointCount(points.count());

        if (pointCount == 0) {
            return;
        }

        QPoint currentPoint = points[0];

        minX = currentPoint.x();
        maxX = minX;
        minY = currentPoint.y();
        maxY = minY;

        int currentX;
        int currentY;

        for (int i = 1; i < points.count(); ++i) {
            currentPoint = points[i];

            if (currentPoint == QGEngine::BreakPoint) {
                continue;
            }

            currentX = currentPoint.x();
            currentY = currentPoint.y();

            if (currentX < minX) {
                minX = currentX;
            }
            else if (currentX > maxX) {
                maxX = currentX;
            }

            if (currentY < minY) {
                minY = currentY;
            }
            else if (currentY > maxY) {
                maxY = currentY;
            }
        }
    }

    /*!
      Calculates and returns the Manhattan distance between \a point1 and
      \a point2.
    */
    static inline int manhattanDistance(const QPoint &point1,
                                        const QPoint &point2)
    {
        return qAbs(point1.x() - point2.x()) + qAbs(point1.y() - point2.y());
    }


    /*!
      Calculates and returns the Euclidean distance between \a point1 and
      \a point2.
    */
    static inline qreal euclideanDistance(const QPoint &point1,
                                          const QPoint &point2)
    {
        return qSqrt(qPow((qreal)(point1.x() - point2.x()), 2.0) +
                     qPow((qreal)(point1.y() - point2.y()), 2.0));

    }

    /*!
      Calculates and returns the Euclidean distance between \a point1 and
      \a point2.
    */
    static inline qreal euclideanDistance(const QPointF &point1,
                                          const QPointF &point2)
    {
        return qSqrt(qPow((qreal)(point1.x() - point2.x()), 2.0) +
                     qPow((qreal)(point1.y() - point2.y()), 2.0));
    }

    /*!
      Copies gestures from \a sourceSet to \a destSet.
    */
    static inline void copyGestureSet(QList<Gesture*> &sourceSet,
                                      QList<Gesture*> &destSet)
    {
        if (destSet.count()) {
            qDeleteAll(destSet);
            destSet.clear();
        }

        const int count(sourceSet.count());
        DEBUG_INFO("Copying" << count << "gestures.");
        Gesture *gesture;

        for (int i = 0; i < count; ++i) {
            gesture = new Gesture(*sourceSet[i]);
            destSet.append(gesture);
        }
    }
};


#endif // QGUTILS_H

// End of file.
